package com.woniu.crowd.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;

public class OssUtil {
    static String id = "LTAI5tDbJzXHuSwyy46dCctP";
    static String secret = "XvBdX4qfcFBvFG7BDiHKKU4KQI90HH";
    static String endPoint ="oss-cn-chengdu.aliyuncs.com";

    /**
     * 上传
     *
     * @param bucketName
     * @param fileName
     * @param file
     * @return
     */
    public static String uploadFile(String bucketName, String fileName, InputStream file) {
        OSS ossClient = new OSSClientBuilder().build("http://" + endPoint, id, secret);
        try {
            ossClient.putObject(bucketName, fileName, file);
            return "http://" + bucketName + "." + endPoint + "/" + fileName;
        } finally {
            if (ossClient != null) ossClient.shutdown();
        }
    }

    /**
     * 删除
     * @param bucketName
     * @param fileName
     * @return
     */
    public static boolean removeFile(String bucketName, String fileName) {
        OSS ossClient = new OSSClientBuilder().build("http://" + endPoint, id, secret);
        try {
            ossClient.deleteObject(bucketName, fileName);
            return true;
        } finally {
            if (ossClient != null) ossClient.shutdown();
        }
    }


}
